package it.asvp.asvp_portal.service.services.impl;

import it.asvp.asvp_portal.asvp_util.util.DateUtils;
import it.asvp.asvp_portal.data.dao.AddressDao;
import it.asvp.asvp_portal.data.dao.MemberAccountDao;
import it.asvp.asvp_portal.data.dao.MemberDao;
import it.asvp.asvp_portal.data.entity.domain.Address;
import it.asvp.asvp_portal.data.entity.member.Member;
import it.asvp.asvp_portal.data.entity.member.MemberAccount;
import it.asvp.asvp_portal.service.services.MemberAccountService;
import it.asvp.asvp_portal.service.services.MemberService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;


@Service
public class MemberServiceImpl implements  MemberService{
	@Autowired
	MemberDao memberDao;
	@Autowired
	AddressDao addressDao;
	@Autowired
	MemberAccountDao memberAccountDao;
	@Autowired
	MemberAccountService memberAccountService;
	
	//FIXME: segnalazione mail
	@Transactional
	@Rollback
	public void createMember(UserDetails userDetails,Member transientMember,MemberAccount transientAccount) throws Exception{
		
		MemberAccount persistentCurrentAccount=memberAccountDao.findByLogin(userDetails.getUsername());
		  
		if(persistentCurrentAccount==null){
				throw new Exception("Account errato");
		}
		
		if(memberDao.findByFiscalCode(transientMember.getFiscalCode())!=null){
			throw new Exception("Codice Fiscale gia' esistente");
		}
		
		if(memberDao.findByEmail(transientMember.getEmail())!=null){
			throw new Exception("Email gia' esistente");
		}
		
		Long idAddress=addressDao.persist(transientMember.getAddress());
	    transientMember.setAddress(addressDao.findById(idAddress));
		Long id=memberDao.persist(transientMember);
		transientAccount.setMember(memberDao.findById(id));
		//invio email al consiglio
		memberAccountService.createAccount(userDetails, transientAccount);
		 //invio mail al consiglio
	}
	
	@Transactional
	public void updateMember(UserDetails userDetails,Long idMemberToUpdate,Member newMemberData) throws Exception{
		
		MemberAccount persistentCurrentAccount=memberAccountDao.findByLogin(userDetails.getUsername());
		  
		if(persistentCurrentAccount==null){
				throw new Exception("Account errato");
		}
		
		Member persistentMemberToUpdate=memberDao.findById(idMemberToUpdate);
		
		if(persistentMemberToUpdate==null){
			throw new Exception("socio da aggiornare non esistente");
		}

		if(memberDao.findByFiscalCode(newMemberData.getFiscalCode())!=null){
			throw new Exception("Codice Fiscale gia' esistente");
		}
		
		if(memberDao.findByEmail(newMemberData.getEmail())!=null){
			throw new Exception("Email gia' esistente");
		}
		
        Address persistentAddress=persistentMemberToUpdate.getAddress();
        persistentAddress.setCity(newMemberData.getAddress().getCity());
        persistentAddress.setCivicNumber(newMemberData.getAddress().getCivicNumber());
        persistentAddress.setLastUpdate(DateUtils.getCurrentDate());
        persistentAddress.setPostalCode(newMemberData.getAddress().getPostalCode());
        persistentAddress.setProvince(newMemberData.getAddress().getProvince());
        persistentAddress.setStreet(newMemberData.getAddress().getStreet());
        
        persistentMemberToUpdate.setAddress(persistentAddress);
        persistentMemberToUpdate.setBirthDate(newMemberData.getBirthDate());
        persistentMemberToUpdate.setEmail(newMemberData.getEmail());
        persistentMemberToUpdate.setFirstName(newMemberData.getFirstName());
        persistentMemberToUpdate.setFiscalCode(newMemberData.getFiscalCode());
        persistentMemberToUpdate.setLastName(newMemberData.getLastName());
        persistentMemberToUpdate.setLastUpdate(DateUtils.getCurrentDate());
        persistentMemberToUpdate.setNationality(newMemberData.getNationality());
        persistentMemberToUpdate.setPhone(newMemberData.getPhone());
        persistentMemberToUpdate.setSchool(newMemberData.getSchool());
        persistentMemberToUpdate.setSex(newMemberData.getSex());
        
        memberDao.merge(persistentMemberToUpdate);
        //invio mail al consiglio
	}
	
	@Transactional
	public void deleteMember(UserDetails userDetails,Member persistentMember) throws Exception{
		
		MemberAccount persistentCurrentAccount=memberAccountDao.findByLogin(userDetails.getUsername());
		  
		if(persistentCurrentAccount==null){
				throw new Exception("Account errato");
		}
		
        Member persistentMemberToDelete=memberDao.findById(persistentMember.getId());
		
		if(persistentMemberToDelete==null){
			throw new Exception("socio da cancellare non esistente");
		}	

        memberDao.delete(persistentMember);
        //invio mail al consiglio
	}
	
	@Transactional
	public Member getMemberById(Long id) throws Exception{
		Member member=memberDao.findById(id);
		if(member==null){
			throw new Exception("Socio non esistente");
		}
		return member;
	}
	
	@Transactional
	public Member getMemberByEmail(String email) throws Exception{
		Member member=memberDao.findByEmail(email);
		if(member==null){
			throw new Exception("Socio non esistente");
		}
		return member;
	}
	
	@Transactional
	public List<Member> getAllMembers() throws Exception{
		return memberDao.findAll();
	}
}
